package com.java.example.bean

import java.lang.ref.ReferenceQueue
import java.lang.ref.SoftReference

class MultipleItemEntity internal constructor(fields: LinkedHashMap<Any, Any>?)  {
    //实际存储数据是在MULTIPLE_FIELDS中，另外另个对象的使用是用来配合软引用
    //所以FIELDS_REFERENCES.get()得到的就是MULTIPLE_FIELDS
    private val ITEM_QUEUE = ReferenceQueue<LinkedHashMap<Any, Any>>()
    private val MULTIPLE_FIELDS = LinkedHashMap<Any, Any>()
    private val FIELDS_REFERENCES = SoftReference(MULTIPLE_FIELDS, ITEM_QUEUE)

    init {
        FIELDS_REFERENCES.get()!!.putAll(fields!!)
    }


    fun <T> getField(key: Any): T? {
        return FIELDS_REFERENCES.get()!![key] as T?
    }

    val fields: LinkedHashMap<*, *>
        get() = FIELDS_REFERENCES.get()!!

    fun setField(key: Any, value: Any) {
        FIELDS_REFERENCES.get()!![key] = value
    }

    companion object {
        fun builder(): MultipleEntityBuilder {
            return MultipleEntityBuilder()
        }
    }
}
